package hl.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import hl.dao.BaseDao;
import hl.model.TCoach;
import hl.model.TCourseDetail;
import hl.model.TMember;
import hl.model.TPlace;
import hl.model.TTraining;
import hl.pageModel.DataGrid;
import hl.pageModel.Training;
import hl.service.TrainingService;

@Service("trainingService")
public class TrainingServiceImpl implements TrainingService {

	private BaseDao<TTraining> trainingDao;

	private BaseDao<TCoach> coachDao;

	private BaseDao<TCourseDetail> courseDetailDao;

	private BaseDao<TPlace> placeDao;
	
	private BaseDao<TMember> memberDao;
	
	public BaseDao<TMember> getMemberDao() {
		return memberDao;
	}

	@Autowired
	public void setMemberDao(BaseDao<TMember> memberDao) {
		this.memberDao = memberDao;
	}
	
	public BaseDao<TPlace> getPlaceDao() {
		return placeDao;
	}

	@Autowired
	public void setPlaceDao(BaseDao<TPlace> placeDao) {
		this.placeDao = placeDao;
	}

	public BaseDao<TCourseDetail> getCourseDetailDao() {
		return courseDetailDao;
	}

	@Autowired
	public void setCourseDetailDao(BaseDao<TCourseDetail> courseDetailDao) {
		this.courseDetailDao = courseDetailDao;
	}

	public BaseDao<TTraining> getTrainingDao() {
		return trainingDao;
	}

	@Autowired
	public void setTrainingDao(BaseDao<TTraining> trainingDao) {
		this.trainingDao = trainingDao;
	}

	public BaseDao<TCoach> getcoachDao() {
		return coachDao;
	}

	@Autowired
	public void setcoachDao(BaseDao<TCoach> coachDao) {
		this.coachDao = coachDao;
	}

	@Override
	public DataGrid getDatagrid(Training training) {
		DataGrid dg = new DataGrid();
		String hql = "from TTraining t ";

		Map<String, Object> params = new HashMap<String, Object>();

		hql = search(training, hql, params);

		if (training.getOrder() != null) {
			hql += " order by " + training.getSort() + " " + training.getOrder();
		}

		List<TTraining> l = trainingDao.find(hql, params, training.getPage(), training.getRows());

		int totalHql = trainingDao.count(hql, params);

		List<Training> pl = new ArrayList();
		if (l != null && l.size() > 0) {
			for (TTraining t : l) {
				Training u = new Training();
				BeanUtils.copyProperties(t, u);
				u.setCoachName(t.getTCoach().getName());
				u.setPkCoach(t.getTCoach().getPkCoach());
				u.setCourseDetailName(t.getTCourseDetail().getName());
				u.setPkCourseDetail(t.getTCourseDetail().getPkCourseDetail());
				u.setPlaceName(t.getTPlace().getName());
				u.setPkPlace(t.getTPlace().getPkPlace());
				u.setMemberName(t.getTMember().getName());
				u.setPkMember(t.getTMember().getPkMember());
				pl.add(u);
			}
		}
		dg.setRows(pl);
		dg.setTotal(totalHql);
		return dg;
	}

	private String search(Training training, String hql, Map<String, Object> params) {
		/*if (training.getName() != null && !training.getName().trim().equals("")) {
			hql += " where t.name like :value1 ";
			params.put("value1", "%" + training.getName().trim() + "%");
		}

		if (training.getCoachName() != null && !training.getCoachName().equals("")) {
			if (hql.contains("where"))
				hql += " and ";
			else
				hql += " where ";
			hql += " t.TCoach.name like :value2 ";
			params.put("value2", "%" + training.getCoachName().trim() + "%");
		}
		if ((training.getBeginDatetime() != null && !training.getBeginDatetime().equals("")) && (training.getEndDatetime() != null && !training.getEndDatetime().equals(""))) {
			if (hql.contains("where"))
				hql += " and ";
			else
				hql += " where ";
			hql += " t.beginDatetime between :value3 and :value4 ";
			params.put("value3", training.getBeginDatetime());
			params.put("value4", training.getEndDatetime());
		}*/
		return hql;
	}

	@Override
	public void save(Training training) {
		TTraining t = new TTraining();
		BeanUtils.copyProperties(training, t);
		t.setCdatetime(new Date());
		t.setTCoach(coachDao.get(TCoach.class, training.getPkCoach()));
		t.setTCourseDetail(courseDetailDao.get(TCourseDetail.class, training.getPkCourseDetail()));
		t.setTPlace(placeDao.get(TPlace.class, training.getPkPlace()));
		t.setTMember(memberDao.get(TMember.class, training.getPkMember()));
		trainingDao.save(t);
	}

	@Override
	public void edit(Training training) {
		TTraining t = new TTraining();
		BeanUtils.copyProperties(training, t);
		t.setMdatetime(new Date());
		t.setTCoach(coachDao.get(TCoach.class, training.getPkCoach()));
		t.setTCourseDetail(courseDetailDao.get(TCourseDetail.class, training.getPkCourseDetail()));
		t.setTPlace(placeDao.get(TPlace.class, training.getPkPlace()));
		trainingDao.saveOrUpdate(t);
	}

	@Override
	public void remove(Training training) {
		TTraining t = trainingDao.get(TTraining.class, training.getPkTraining());
		trainingDao.delete(t);
	}

}
